//
//  TXFMusicManager.m
//  FFMusic
//
//  Created by 田侠飞 on 16/7/19.
//  Copyright © 2016年 田侠飞. All rights reserved.
//

#import "TXFMusicManager.h"
#import <AVFoundation/AVFoundation.h>


@interface TXFMusicManager ()


@property(nonatomic,strong)AVAudioPlayer * audioPlayer;

@property(nonatomic,copy)NSString * currentFilename;

@end
static TXFMusicManager * instance;
@implementation TXFMusicManager

+(instancetype)shareManager{

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
    
        instance = [[TXFMusicManager alloc]init];
    
    });
    return instance;

}
-(void)playMusicWithFilename:(NSString *)filename{

    if (self.currentFilename!= filename) {
        
        
        NSString * filePath = [[NSBundle mainBundle]pathForResource:filename ofType:nil];
        
        
        self.audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:filePath] error:nil];
        
        [self.audioPlayer prepareToPlay];
        
        self.currentFilename = filename;
    }
  dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)),
        dispatch_get_main_queue(), ^{
            
            [self.audioPlayer play];
  });
}

-(void)pauseMusic{

    [self.audioPlayer pause];

}

- (NSTimeInterval)duration{
    //获取歌曲时长
    return self.audioPlayer.duration;
}

- (NSTimeInterval)currentTime{
    //获取当前时间
    return  self.audioPlayer.currentTime;
}

- (void)setCurrentTime:(NSTimeInterval)currentTime{
    
    //手动调整播放进度
    self.audioPlayer.currentTime = currentTime;
}
@end
